package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserGroupBean;
import com.koron.css2.clientCenter.mapper.UserGroupMapper;
import com.koron.css2.clientCenter.queryBean.userGroupQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class UserGroupSave implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		// TODO Auto-generated method stub
		MessageBean<String> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", String.class);
		try {
			UserGroupBean bean = JsonUtils.objectToPojo(req.getData(), UserGroupBean.class);
			
			UserGroupMapper mapper = factory.getMapper(UserGroupMapper.class);
			
			//id为空直接插入,否则更新
			if(StringUtils.isEmpty(bean.getId())) {
			//判断组编号是否重复
			userGroupQueryBean queryBean = new userGroupQueryBean();
			queryBean.setFuzzyQuery(bean.getUserGroupNo());
			List<UserGroupBean> list = mapper.selectList(queryBean);
			if(list!=null && list.size()>0) {
				return  MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败,组编号已存在", null);
			}
				bean.setCreateInfo(userInfo);
				mapper.add(bean);
			}else {
				bean.setUpdateInfo(userInfo);
				mapper.update(bean);
			}
			
			return info;
		}catch (Exception e) {
			// TODO: handle exception
			logger.error("用户组保存失败" + e.getMessage(), e);
			return  MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
		}
	}

}
